window.onload = function(){
    var list = document.getElementById('userName');
    var user = localStorage.getItem('user')
    var username = JSON.parse(user);
    if (username !== null) {
        var li = document.createElement('li');
        var a = document.createElement('a');
        a.innerHTML = 'welcome' + username.data.username;
        li.appendChild(a);
        list.appendChild(li);
    }
}
var bookString = localStorage.getItem('bookIformation');
var bookArr = JSON.parse(bookString);
var pic = document.getElementById('tu');
var font = document.getElementById('wor');
var money1 =document.getElementById('money1');
money1.innerHTML = bookArr.book_price;
// console.log(bookArr.book_img);
pic.src = 'http://192.168.10.102:8888/'+ bookArr.book_img;
font.innerHTML = bookArr.book_name;
// 以上载入本地存储
// 以下数量加减
document.getElementById("num").innerHTML = 1;
function add(){
document.getElementById('num').innerHTML++;
bookArr.book_number++;
localStorage.setItem('bookIformation',JSON.stringify(bookArr));
}
function reduce(){
    if(document.getElementById('num').innerHTML == 1){
        document.getElementById('num').innerHTML = 1;
        bookArr.book_number =1;
        localStorage.setItem('bookIformation',JSON.stringify(bookArr));
    }else{
        document.getElementById('num').innerHTML--;
        bookArr.book_number--;
        localStorage.setItem('bookIformation',JSON.stringify(bookArr));
    }
}
localStorage.setItem('bookIformation',JSON.stringify(bookArr));
var mount = document.getElementById('num');
function inCart(){

}
// 以下加入购物车
var allBooksArray = [];
function inCart() {
    var bookString = localStorage.getItem('bookIformation');
var bookArr = JSON.parse(bookString);
    var cartBook = {
        book__id: bookArr.book_id,
        book__img:bookArr.book_img,
        book__name: bookArr.book_name,
        book__price: bookArr.book_price,
        book__number:bookArr.book_number
    }
    var user = localStorage.getItem('user')
    var username = JSON.parse(user);
    if (username != null) {
        var allBooks_json = localStorage.getItem('allBooks');
        console.log(allBooks_json);
        if (allBooks_json != null) {
            allBooksArray = JSON.parse(allBooks_json);
        }
        var cartNumber = true;

        for (var i = 0; i < allBooksArray.length; i++) {
            if (allBooksArray[i].book__id === bookArr.book_id) {
                allBooksArray[i].book__number += bookArr.book_number;
                cartNumber = false;
                break;
            }
        }

        if (cartNumber) {
            allBooksArray.push(cartBook);
        }

        localStorage.setItem('allBooks', JSON.stringify(allBooksArray));

    } else {
        alert('请登录后再操作');
        window.location = "http://127.0.0.1:5500/html/login.html";

    }
}